package com.malamute.core.tag;

import java.io.IOException;
import java.io.Writer;

import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * @Title:Pages.java
 * @Package com.malamute.core.tag
 * @Description 分页逻辑Bean
 * @author AceGuan gjybst@163.com
 * @date 2012-1-31 下午09:52:38
 * @version v1.0
 */
public class Pages extends Component {
	private String cpage;// 当前页
	private String tpage;// 总页面
	private String url;// 请求地址

	public static final Integer showPage = 5;// 一共显示为6个点击

	/**
	 * 获取cpage
	 * 
	 * @return cpage
	 */

	public String getCpage() {
		return cpage;
	}

	/**
	 * 设置cpage
	 * 
	 * @param cpage
	 */
	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	/**
	 * 获取tpage
	 * 
	 * @return tpage
	 */

	public String getTpage() {
		return tpage;
	}

	/**
	 * 设置tpage
	 * 
	 * @param tpage
	 */
	public void setTpage(String tpage) {
		this.tpage = tpage;
	}

	/**
	 * 获取url
	 * 
	 * @return url
	 */

	public String getUrl() {
		return url;
	}

	/**
	 * 设置url
	 * 
	 * @param url
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @Title:Pages
	 * @Description:TODO
	 * @param @param stack
	 */
	public Pages(ValueStack stack) {
		super(stack);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param writer
	 * 
	 * @return
	 * 
	 * @see org.apache.struts2.components.Component#start(java.io.Writer)
	 */
	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try {
			StringBuilder str = new StringBuilder();
			boolean isValid = true;

			if (isValid) {
				if (tpage.startsWith("%{") && tpage.endsWith("}")) {
					tpage = tpage.substring(2, tpage.length() - 1);
					tpage = String.valueOf(this.getStack().findValue(tpage));
					isValid = tpage == null ? false : true;
				} else {
					isValid = false;
				}
			}
			if (isValid) {
				if (cpage.startsWith("%{") && cpage.endsWith("}")) {
					cpage = cpage.substring(2, cpage.length() - 1);
					cpage = String.valueOf(this.getStack().findValue(cpage));
					isValid = cpage == null ? false : true;
				} else {
					isValid = false;
				}
			}
			if (isValid) {
				if (url.startsWith("%{") && url.endsWith("}")) {
					url = url.substring(2, url.length() - 1);
					url = (String) this.getStack().findValue(url);
					isValid = url == null ? false : true;
				} else {
					isValid = false;
				}
			}
			if (isValid) {
				// String
				// test=String.valueOf(this.getStack().findValue("page.totalNum"));

			}

			// 参数都正确的情况下，开始进行分页逻辑处理
			if (isValid) {
				Integer currentPage = Integer.valueOf(cpage) + 1;// 当前页面
				Integer totalPage = Integer.valueOf(tpage);// 总页面
				System.out.println("当前页数：" + cpage);
				System.out.println("currentPageNum：" + currentPage);
				str.append("<div class='pagelist'>");
				str.append("<span>共"
						+ tpage
						+ " 页/"
						+ String.valueOf(this.getStack().findValue("page.totalNum"))
						+ "条记录</span>");
				int fromNum = 1;
				int toNum = 0;
				if (currentPage - 2 <= 0) {
					if (showPage <= totalPage) {
						// 0,showpage
						toNum = showPage;
						// str.append(createPagePart(1, showPage, currentPage));
					} else {
						// 0,totalPage
						toNum = totalPage;
						// str.append(createPagePart(1, totalPage,
						// currentPage));
					}
				} else {
					if (currentPage + 2 <= totalPage) {
						// currentPage-5,totalPage
						fromNum = currentPage - 2;
						toNum = currentPage + 2;
					} else {
						fromNum = currentPage - 2
								- (currentPage + 2 - totalPage);
						toNum = totalPage;
						// currentPage-2,currentPage+2
						// str.append(createPagePart(currentPage - 2,
						// currentPage + 2, currentPage));
					}
				}
				str.append(createPagePart(fromNum, toNum, currentPage));
			}
			str.append("</div>");
			writer.write(str.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	private String createPagePart(int fromNum, int toNum, int currentPage) {
		StringBuffer sb = new StringBuffer();
		if (fromNum <= 0) {
			fromNum = 1;
		}
		for (int i = fromNum; i <= toNum; i++) {
			if (i == currentPage) {
				sb.append("<strong style='margin-left:5px''>");
				sb.append(currentPage);
				sb.append("</strong>");

			} else {
				sb.append("<a style='margin-left:5px' href='" + url + "?page.currentPageNum=" + (i - 1)
						+ "'>");
				sb.append(i);
				sb.append("</a>");
			}
		}
		return sb.toString();
	}
}
